<template>
    <section>
        <!--工具条-->

        <div class="section-content clearfix">
            <el-col :span="24" style="padding-bottom: 0px;">
                <el-form :inline="true" :model="filters">

                    <el-form-item>
                        <el-select v-if="usertype==2" v-model="scade1" filterable placeholder="所属组织" @change="getUsers()"
                            class="">
                            <el-option key="" label="全部" value=" "></el-option>
                            <el-option v-for="item in firmlist" :key="item.companyId" :label="item.name" :value="item.companyId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="filters.name" placeholder="用户名" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="filters.phone" placeholder="电话号码" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                    <!-- <el-form-item v-if="menubt.indexOf('/user/add_child')>-1">
                        <el-button :type="buttonColor" @click="handleAdd">新增</el-button>
                    </el-form-item> -->
                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="users" highlight-current-row class="alarmshistory" :header-row-style="{backgroundColor: titleColor}" v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
                <!-- <el-table-column type="selection">
                </el-table-column> -->
                <el-table-column type="index" label="序号"  width="80":index="indexMethod">
                </el-table-column>
                <el-table-column prop="userName" label="用户名">
                </el-table-column>
                <el-table-column prop="nickName" label="昵称">
                </el-table-column>
                <el-table-column prop="lastLoginTime" label="时间">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="phone" label="电话号码">
                </el-table-column>
                <el-table-column prop="company.name" label="所属组织">
                </el-table-column>
                <el-table-column label="用户属性">
                    <template scope="scope">
                        <span v-if="scope.row.authType==0">管理员</span>
                        <!-- <span v-if="scope.row.authType==1" style="color:#0099cc;cursor: pointer;" @click="employee_bind(scope.$index, scope.row)">员工</span> -->
                        <span v-if="scope.row.authType==2">维修员</span>
                        <span v-if="scope.row.authType==1">员工</span>
                        <span v-if="scope.row.authType==3">消费端用户</span>

                    </template>
                </el-table-column>
                <el-table-column label="当前状态">
                    <template scope="scope">
                        <span v-if="scope.row.status==1">已停用</span>
                        <span v-if="scope.row.status==2">已启用</span>
                    </template>
                </el-table-column>
                <el-table-column label="状态操作">
                    <template scope="scope">

                        <el-button v-if="menubt.indexOf('user/edit_child')>-1&&scope.row.status==2" size="small" @click="stateput( scope.row)"
                            type="small" plain>停用</el-button>
                        <el-button v-if="menubt.indexOf('user/edit_child')>-1&&scope.row.status==1" size="small" :type="buttonColor"
                            @click="stateput( scope.row)" plain>启用</el-button>
                    </template>
                </el-table-column>
                <el-table-column v-if="custom_version!=1" label="登录地址">
                    <template scope="scope">
                        <div style="width:60px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis" :title="scope.row.address">{{scope.row.address}}</div>
                    </template>
                </el-table-column>
                <el-table-column align="center" label="角色">
                    <template scope="scope">

                        <el-button v-if="menubt.indexOf('/user/edit_child_role')>-1" size="small" @click="exentEdit(scope.$index, scope.row)">关联角色</el-button>
                        <!-- <el-button size="small" @click="companyedit(scope.$index, scope.row)">厂商信息</el-button> -->
                    </template>
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                        <!-- <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button> -->
                        <i v-if="menubt.indexOf('/user/edit_child')>-1" @click="handleEdit(scope.$index, scope.row)"
                            title="编辑" icon="el-icon-edit" circle class="pb-icon icon-edit"></i>
                        <i v-show="scope.row.authType!=0" @click="handleDel(scope.$index, scope.row)"
                            icon="el-icon-delete" circle title="删除" class="pb-icon icon-delete"></i>

                    </template>
                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"   @size-change="handleSizeChange" :page-size="10" :total="total"
                    style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible = false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="用户名" prop="userName">
                        <el-input v-model="editForm.userName" auto-complete="off" :disabled="editForm.authType==3"></el-input>
                    </el-form-item>
                    <!-- <el-form-item label="密码">
                        <el-input type="text" v-model="editForm.password" disabled></el-input>
                    </el-form-item> -->
                    <el-form-item label="手机号码" prop="phone">
                        <el-input type="number" v-model="editForm.phone" :disabled="editForm.authType==3"></el-input>
                    </el-form-item>

                    <el-form-item label="邮箱">
                        <el-input v-model="editForm.email" :disabled="editForm.authType==3"></el-input>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input  v-model="editForm.address" ></el-input>
                    </el-form-item>

                    <el-form-item label="机构名称">
                        <el-input v-model="editForm.userCompany"></el-input>
                    </el-form-item>
                    <el-form-item label="用户属性">
                        <!-- <span v-if="editForm.authType==0">客户</span> -->
                        <span v-if="editForm.authType==1">员工</span>
                        <span v-if="editForm.authType==2">维修</span>
                        <span v-if="editForm.authType==3">公众号用户 (不可修改)</span>
                     
                    </el-form-item>
                 
                    <el-form-item label="重置密码">
                        <el-input v-model="passWord" :disabled="editForm.authType==3"></el-input>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">

                <!-- <el-button :type="buttonColor" @click.native="reset_passwords(editForm.childUserId)" :disabled="editForm.authType==3"
                    :loading="editLoading">重置密码</el-button> -->
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button :type="buttonColor" @click.native="editSubmit(editForm.childUserId)" :disabled="editForm.authType==3" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
        <el-dialog title="组织信息编辑" v-show="companyVisible" :custom-class="dialogTitleColor" @close="companyVisible = false">
            <div slot="footer" style="text-align: left; ">
                <el-form ref="form" :model="form" label-width="100px" @submit.prevent="onSubmit">
                    <el-form-item label="组织名" prop="userName">
                        <el-input v-model="form.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="电话号码" prop="phone">
                        <el-input v-model="form.phone" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="经度">
                        <el-input v-model="form.longitude"></el-input>
                    </el-form-item>
                    <el-form-item label="纬度">
                        <el-input v-model="form.latitude"></el-input>
                    </el-form-item>
                    <el-form-item label="地址">
                        <el-input v-model="form.address" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="网址">
                        <el-input v-model="form.site" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" v-model="form.remark"></el-input>
                    </el-form-item>

                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="companyVisible = false">取消</el-button>
                <el-button :type="buttonColor" @click.native="companySubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>
        <!--权限编辑界面-->
        <el-dialog title="关联角色" v-show="editexentble" :custom-class="dialogTitleColor" @close="editexentble = false">
            <div slot="footer" style="text-align: left; ">

                <el-transfer v-model="editrole.roleList" :titles="['未选', '已选']" :button-texts="['到左边', '到右边']" :data="userlists"
                    style="margin-left:30px;padding-bottom:20px;"></el-transfer>

            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editexentble = false">取消</el-button>
                <el-button :type="buttonColor" @click.native="editSubmitrole" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible = false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="editFormRules" ref="addForm">
                    <el-form-item label="用户名" prop="userName">
                        <el-input v-model="addForm.userName" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号码" prop="phone">
                        <el-input type="number" v-model="addForm.phone"></el-input>
                    </el-form-item>
                    <el-form-item label="密码">
                        <el-input type="password" v-model="addForm.password"></el-input>
                    </el-form-item>
                    <el-form-item label="邮箱">
                        <el-input v-model="addForm.email"></el-input>
                    </el-form-item>
                    <el-form-item label="关联角色">
                        <el-select v-model="addForm.roleList" multiple placeholder="请选择">
                            <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="用户属性选择">
                        <el-radio-group v-model="addForm.authType">
                            <!-- <el-radio label="0">客户</el-radio> -->
                            <el-radio label="1">员工</el-radio>
                            <el-radio label="2">维修</el-radio>
                        </el-radio-group>
                    </el-form-item>

                </el-form>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align: right;">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <el-dialog title="客户绑定" v-show="employeeform" :custom-class="dialogTitleColor" @close="employeeform = false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

                    <el-form-item label="客户选择">
                        <el-select v-model="employeelist" multiple placeholder="请选择">
                            <el-option v-for="item in bindelist" :key="item.userId" :label="item.userName" :value="item.userId">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer" style="text-align: right;">
                <el-button @click.native="employeeform = false">取消</el-button>
                <el-button :type="buttonColor" @click.native="employee_sb" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
        <!-- 验证码 -->
        <el-dialog title="删除" v-show="delVisible" v-model="delVisible" :custom-class="dialogTitleColor" @close="delVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="addForm" label-width="100px" :rules="addFormRules"
                    ref="addForm">
                    <el-form-item label="验证码">
                        <el-input v-model="verifyCode" auto-complete="off"></el-input>
                    </el-form-item>


                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">

                    <el-button type="success" @click.native="codeadd">获取验证码</el-button>
                    <el-button @click.native="delVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="delclick" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    //import NProgress from 'nprogress'
    import {
        query_user,
        user_query,
        query_userpost,
        query_userput,
        query_userdelete,
        query_userpermission,
        user_add_child,
        user_edit_child,
        user_edit_child_role,
        user_role_query_child_user,
        user_get_childr,
        company_get,
        company_put,
        company_post,
        user_reset_child_pass,
        user_enable,
        user_disable,
        user_delete_child,
        employee_bind_customer_query,
        employee_bind_customer_batchBindCustomer,
        send_auth_pass_code,
        user_querycompany
    } from '../../../api/api';

    export default {
        data() {
            const generateDatas = _ => {
                const data = [];
                let para = {

                    pageNo: 1,
                    pageSize: 1000
                };
                //NProgress.start();
                query_user(para).then((res) => {
                     this.begCode(res.code,res.message);
                    // this.total = res.data.totalCount;
                    if (res.code == 0) {
                        for (let i in res.data.list) {
                            data.push({
                                key: res.data.list[i].id,
                                label: res.data.list[i].name
                            });
                        }
                    } else {
                        this.$router.push({
                            path: '/login'
                        });
                    }
                    //NProgress.done();
                });
                return data;
            };
            return {
                custom_version: this.CustomVersion(),//为0正常公有云，为1私有化定制
                verifyCode:'',
                scade1:'',
                firmlist:'',
                usertype:'',
                passWord: '',
                form: {
                    name: '',
                    remark: '',
                    site: '',
                    phone: '',
                    longitude: '',
                    latitude: '',
                    address: '',
                    companyUserId: '',
                },
                userlists: generateDatas(),
                defaultProps: {
                    children: 'children',
                    label: 'name',
                    isLeaf: 'checked'

                },
                delVisible:false,
                roleList: '',
                filters: {
                    name: '',
                    phone: ''
                },
                users: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                exentform: [],
                editexentble: false, //权限编辑页面是否显示
                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    userName: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    phone: [{
                        required: true,
                        message: '请输入手机号码',
                        trigger: 'blur'
                    },  {
              pattern: /^((1[3589][0-9])|(14[57])|(17[03678])|(19[7]))\d{8}$/,
              message: "请填写正确手机号码",
              trigger: "blur"
            }]
                },
                //编辑界面数据
                editForm: {
                    childUserId: 0,
                    userName: '',
                    remark: '',
                    address: '',
                    phone: '',
                    authType: '',
                    address:'',
                    userCompany:''
                },
                editrole: {
                    childUserId: '',
                    roleList: [],
                },
                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    userName: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    phone: [{
                        required: true,
                        message: '请输入手机号码',
                        trigger: 'blur'
                    },  {
              pattern: /^((1[3589][0-9])|(14[57])|(17[03678])|(19[7]))\d{8}$/,
              message: "请填写正确手机号码",
              trigger: "blur"
            }]
                },
                //新增界面数据
                addForm: {
                    userName: '',
                    phone: '',
                    password: "",
                    email: '',
                    roleList: [],
                    password: '',
                    authType: '0'

                },
                companyVisible: false,
                employeeform: false,
                employeelist: [],
                bindelist: [],
                employeeUserId: '',
                userId:''
            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            codeadd() {
                console.log(this.dellist)
          
                send_auth_pass_code().then((res) => {

                    this.begCode(res.code, res.message);
                    //NProgress.done();
                    if (res.code == 0) {
                        this.$message({
                            message: '发送成功,请耐心等待',
                            type: 'success'
                        });
                    }

                });
            },
            handleSizeChange(val) {
                this.pageSize = val;

                this.getUsers();
            },
            
            
            employee_sb() {
                console.log(this.employeelist);
                let para = {
                    employeeUserId: this.employeeUserId,
                    idList: this.employeelist
                }

                employee_bind_customer_batchBindCustomer(para).then((res) => {

                     this.begCode(res.code,res.message);
                    if (res.code == 0) {

                        this.employeeform = false;
                        //NProgress.done();


                    }
                });
            },
            employee_bind(index, row) {
                this.employeeform = true;
                let para = {
                    employeeUserId: row.userId
                }
                this.employeeUserId = row.userId
                employee_bind_customer_query(para).then((res) => {

                     this.begCode(res.code,res.message);
                    if (res.code == 0) {
                        this.employeelist = res.data.list
                        //NProgress.done();


                    }
                });
            },
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            stateput(s) {
                let para = {
                    idList: [s.userId]
                }
                if (s.status == 1) {

                    user_enable(para).then((res) => {

                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            //NProgress.done();
                            this.$message({
                                message: '启用成功',
                                type: 'success'
                            });

                            this.getUsers();

                        }
                    });
                } else {

                    user_disable(para).then((res) => {

                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            //NProgress.done();
                            this.$message({
                                message: '停用成功',
                                type: 'success'
                            });

                            this.getUsers();

                        }
                    });
                }

            },
            reset_passwords(s) {
                this.$confirm('确认重置密码吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    //NProgress.start();
                    if (this.passWord.length < 6) {
                        this.$message({
                            message: '修改的密码最小为6位',
                            type: 'warning'
                        });
                        return false;
                    }
                    let para = {
                        childUserId: s,
                        passWord: this.passWord
                    };
                    user_reset_child_pass(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {
                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '重置密码成功',
                                type: 'success'
                            });

                            this.editFormVisible = false;
                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            companySubmit() {
                let para = this.form;
                console.log(this.form)
                // return false;
                if (this.form.id) {

                    company_put(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.companyVisible = false;
                        this.getUsers();
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });
                    });
                } else {
                    company_post(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.companyVisible = false;
                        this.getUsers();
                        this.$message({
                            message: '添加成功',
                            type: 'success'
                        });
                        //NProgress.done();
                    });
                }
            },
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            //获取用户列表
            getUsers() {

                if (this.menubt.indexOf('/user/query') == -1) {
                    this.$message({
                        message: '您没有查询子用户的权限',
                        type: 'warning'
                    });
                    return false;
                }
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    // type: 2,
                    phone: this.filters.phone,
                    userName: this.filters.name,
                    companyId: this.scade1
                };
                this.listLoading = true;
                //NProgress.start();
                user_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.page = res.data.pageNo;
                    this.total = res.data.totalCount;
                    // console.log(res);
                    this.users = res.data.list;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            //删除
            delclick(){
                    //NProgress.start();
                    if(!this.verifyCode){
                        this.$message({
                                message: '请输入验证码',
                                type: 'warning'
                            });
                            return false;
                    }
                    let para = {
                        childUserId: this.userId,
                        verifyCode:this.verifyCode
                    };
                    user_delete_child(para).then((res) => {
                        this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.delVisible = false;
                            this.verifyCode='';
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });

                            this.getUsers();

                        }
                    });
            },
            handleDel: function (index, row) {
                this.userId=row.userId;
                this.delVisible = true;
                return false;
                this.$confirm('确认删除该用户吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        childUserId: row.userId
                    };
                    user_delete_child(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });

                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.passWord='';
                let para = {
                    childUserId: row.userId
                }
                user_get_childr(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.editrole.childUserId = row.userId;
                    console.log(res)

                    res.data.status = res.data.status.toString();
                    res.data.authType = res.data.authType.toString();
                    this.editFormVisible = true;
                    this.editForm = {
                        childUserId: res.data.userId,
                        email: res.data.email,
                        phone: res.data.phone,
                        remark: res.data.remark,
                        address: res.data.address,
                        userName: res.data.userName,
                        status: res.data.status,
                        authType: res.data.authType,
                        userCompany:res.data.userCompany,
                        address:res.data.address,
                        dataRestrict: res.data.dataRestrict
                    }
                })
            },
            companyedit(index, row) {

                this.companyVisible = true;
                let para1 = {
                    companyId: row.companyId
                }

                company_get(para1).then((res) => {
                     this.begCode(res.code,res.message);
                    //    this.form=res
                    if (res.data) {
                        this.form = res.data;

                    } else {
                        this.form = {
                            name: '',
                            remark: '',
                            site: '',
                            phone: '',
                            longitude: '',
                            latitude: '',
                            address: '',
                            companyUserId: row.userId,
                        }
                    }
                })
            },
            exentEdit: function (index, row) {


                this.editrole.roleList = [];
                this.editexentble = true;
                let para1 = {
                    childUserId: row.userId
                }
                user_role_query_child_user(para1).then((res) => {
                     this.begCode(res.code,res.message);
                    this.editrole.childUserId = row.userId;
                    for (let i in res.data.list) {

                        this.editrole.roleList.push(res.data.list[i].id);
                    }
                    console.log(res)
                })
            },
            //显示新增界面
            handleAdd: function () {

                this.addFormVisible = true;
                this.addForm = {
                    userName: '',
                    phone: '',
                    password: "",
                    email: '',
                    roleList: [],
                    password: '',
                    authType: '0'
                };
            },
            //         renderContent(h, { node, data, store }) {
            //     return (
            //       <span class="custom-tree-node">
            //         <span>{node.label}</span>
            //         <span>
            //           <el-button size="mini" type="text" on-click={ () => this.append(data) }>Append</el-button>
            //           <el-button size="mini" type="text" on-click={ () => this.remove(node, data) }>Delete</el-button>
            //         </span>
            //       </span>);
            //   }
            //编辑
            editSubmitrole: function () {
                this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    this.editLoading = true;
                    //NProgress.start();
                    let para = this.editrole;
                    user_edit_child_role(para).then((res) => {
                        this.begCode(res.code, res.message);

                        this.editLoading = false;
                        if (res.code == 0) {

                            //NProgress.done();
                            this.$message({
                                message: '提交成功',
                                type: 'success'
                            });
                            this.$refs['editForm'].resetFields();

                            this.editexentble = false;
                            this.getUsers();

                        }
                    });
                });
            },
            editSubmit: function (s) {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            //NProgress.start();
                            let para = this.editForm;
                            user_edit_child(para).then((res) => {
                                this.begCode(res.code, res.message);
                                this.editLoading = false;
                                if (res.code == 0) {
                                    if(this.passWord != ""){
                                        this.reset_passwords(s)
                                    }else{
                                    //NProgress.done();
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                    }
                                } else {
                                    this.$message({
                                        message: res.message,
                                        type: 'warning'
                                    });
                                }
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                console.log(this.addForm)
                console.log(this.$refs.addForm)
                this.$refs.addForm.validate((valid) => {
                    
                    if (valid) {
                        if(!this.addForm.userName){
                            this.$message({
                                        message: '请填写用户名',
                                        type: 'warning'
                                    });
                                    return false;
                        }
                        if(!this.addForm.phone){
                            this.$message({
                                        message: '请填写手机号码',
                                        type: 'warning'
                                    });
                                    return false;
                        }
                        console.log(valid)
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            for (var i in this.addForm.roleList) {
                                this.addForm.roleList[i] = parseInt(this.addForm.roleList[i]);
                            }
                            let para = Object.assign({}, this.addForm);

                            user_add_child(para).then((res) => {
                                console.log(res,"====================================================")
                                this.begCode(res.code, res.message);
                                if (res.code == 0) {
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['addForm'].resetFields();
                                    this.addFormVisible = false;
                                    this.getUsers();
                                }
                                this.addLoading = false;
                                //NProgress.done();

                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        ids: ids
                    };
                    batchRemoveUser(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            
            this.usertype = JSON.parse(sessionStorage.getItem('user')).type;
            this.menubt = localStorage.getItem('menubt');
            this.getUsers();
            let para = {
                pageNo: 1,
                pageSize: 1000
            };
            this.listLoading = true;
            //NProgress.start();
            query_user(para).then((res) => {
                 this.begCode(res.code,res.message);
                // this.total = res.data.total;
                // console.log(res);
                this.roleList = res.data.list;
                this.listLoading = false;
                //NProgress.done();
            });
            
            let parauser = {
                pageNo: 1,
                pageSize: 9999,
                type: 1
            }
            
            user_querycompany(parauser).then((res) => {

                 this.begCode(res.code,res.message);
                this.firmlist = res.data.list;
                // console.log(this.firmlist)
                // if (this.firmlist[0]) {

                //     this.scade1 = this.firmlist[0].companyId;
                // }

                // this.select_scade();

            });

        }
    }
</script>

<style scoped>

</style>